<template>
  <div>
    <div style="height:28px;padding:5px">
      <el-button-group>
        <el-button size="mini" type="primary" @click="popEdtBox" icon="el-icon-edit">编辑</el-button>
      </el-button-group>
    </div>
    <div>
      <hr style="border:1 dashed " width="100%" color="#DCDFE6" size="1">
    </div>
    <div>
      <ag-grid-vue
        ref="grid"
        :columnDefs="columnDefs"
        :rowData="rowData"
        :style="{ height: gridHeight }"
        :pagination="false"
        :getRowNodeId="function(params){return params.AutoId}"
        row-selection="single"
        class="ag-theme-material"
        @grid-ready="onGridReady"
        @rowDoubleClicked="popEdtBox"
      ></ag-grid-vue>
      <el-pagination
        :total="rowsCount"
        :current-page="currentPage"
        :page-sizes="[10, 15, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="currentPageSelect"
      ></el-pagination>
    </div>
    <DimensionEditor
      ref="edtBox"
      :dimensionmodel="dimensionModel"
      @dimensionSaveComplete="dimensionSaved"
    />
  </div>
</template>

<script>
import { AgGridVue } from 'ag-grid-vue'
import DimensionPricingStrategyRenderer from '@/components/CellRenderer/DimensionPricingStrategyRenderer.vue'
import DimensionEditor from '@/components/EditDialog/DimensionEditor.vue'
import { listDimension } from '@/api/dimension'

export default {
  components: {
    AgGridVue,
    DimensionPricingStrategyRenderer,
    DimensionEditor
  },
  data() {
    return {
      columnDefs: [
        { headerName: '', field: 'chk', width: 50, checkboxSelection: true },
        { headerName: '编码', field: 'Code', sortable: true, filter: true },
        { headerName: '名称', field: 'Name', sortable: true, filter: true },
        { headerName: '序号', field: 'Level', sortable: true, filter: true },
        {
          headerName: '策略',
          field: 'PricingStrategy',
          cellRendererFramework: 'DimensionPricingStrategyRenderer',
          sortable: true,
          filter: true
        }
      ],
      rowData: null,
      gridHeight: '',
      rowsCount: null,
      currentPage: 1,
      pageSize: 15,
      gridApi: null,
      columnApi: null,
      dimensionModel: null
    }
  },
  methods: {
    onGridReady(params) {
      this.gridApi = params.api
      this.columnApi = params.columnApi
    },
    currentPageSelect(pageIdx) {
      if (pageIdx !== this.currentPage) {
        this.currentPage = pageIdx
      }
      const start = (pageIdx - 1) * this.pageSize
      const end = start + this.pageSize
      this.listDimension()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPageSelect(1)
    },
    listDimension() {
      listDimension()
        .then(response => {
          this.rowData = response.Data
          this.rowsCount = response.Cnt
        })
        .catch(error => {
          console.log(error)
          this.rowData = []
          this.rowsCount = 0
        })
    },
    popSchBox() {
      this.$refs.schBox.dialogVisible = true
    },
    popEdtBox() {
      const selectedNodes = this.gridApi.getSelectedNodes()
      const selectedData = selectedNodes.map(node => node.data)
      if (selectedData.length !== 1) {
        this.$alert('请只选中一行记录', '提示')
        return
      } else {
        this.dimensionModel = this.rowData.find(function(item) {
          return item.AutoId === selectedData[0].AutoId
        })
      }
      this.$refs.edtBox.dialogVisible = true
    },
    dimensionSaved(params) {
      if (params.Flag === 1) {
        this.rowData.push(params)
      } else {
        this.gridApi.getRowNode(params.AutoId).setData(params)
      }
    }
  },
  mounted() {
    this.gridHeight = window.innerHeight - 30 - 50 - 32 - 5 - 38 - 10 + 'px'
    const that = this
    window.onresize = function changeGridSize() {
      that.gridHeight = window.innerHeight - 30 - 50 - 32 - 5 - 38 - 10 + 'px'
    }
    this.listDimension()
  },
  beforeMount() {}
}
</script>

<style lang="scss">
@import "../../../node_modules/ag-grid-community/dist/styles/ag-grid.css";
@import "../../../node_modules/ag-grid-community/dist/styles/ag-theme-balham.css";
@import "../../../node_modules/ag-grid-community/dist/styles/ag-theme-material.css";
</style>

